package com.tarena.luban.worker.server.repo.impl;

import com.alibaba.fastjson.JSON;
import com.tarena.luban.worker.server.bo.WorkerBO;
import com.tarena.luban.worker.server.dao.WorkerDao;
import com.tarena.luban.worker.server.repo.AbstractCacheRepository;
import com.tarena.luban.worker.server.repo.WorkerRepository;
import com.tarena.luban.worker.server.repo.converter.WorkerConverter;
import com.tarena.tp.luban.worker.po.Worker;
import com.tarena.tp.luban.worker.server.dto.param.WorkerCreateParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

@Repository
public class WorkerRepoImpl extends AbstractCacheRepository<WorkerBO> implements WorkerRepository {
    //注入一个dao持久层实现对象
    //写入到数据库 sql insert
    @Autowired
    private WorkerDao workerDao;
    //转化器注入
    @Autowired
    private WorkerConverter workerConverter;
    @Override
    public Long saveWorker(WorkerCreateParam workerCreateParam) {
        // param作为规定入参的类型,不是写入数据库,对应表格的类型
        // 对应表格应该是 entity model po
        // 将param参数 能转化到worker的都转化,没有的在这里补齐
        Worker worker = workerConverter.param2po(workerCreateParam);
        workerDao.insert(worker);
        //mybatis持久层框架可以帮助自增主键 自动封装
        return worker.getId();
    }
    // redisTemplate value值 string的value list的元素value hash value set元素value
    // 可以使用object对象 set("user",new User()) 不能执行incr命令
    // stringRedisTemplate 所有value值必须是java的String类型
    // set("user",JSON(new User)); JSONPARSE((get("user"))
    @Override
    public WorkerBO getWorkerByUserId(Long userId) {
        String workerKey="worker:detail:bo:"+userId;
        Boolean hasKey = stringRedisTemplate.hasKey(workerKey);
        if (hasKey){
            return getCache(workerKey);
        }else{
            Worker worker=workerDao.selectByUserId(userId);
            if (worker==null){
                return null;
            }
            WorkerBO workerBO = workerConverter.po2bo(worker);
            setCache(workerKey,workerBO);
            return workerBO;
        }
        /*//1. 检查缓存是否命中
        String workerKey="worker:detail:bo:"+userId;
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        Boolean hasKey = stringRedisTemplate.hasKey(workerKey);//exists workerKey
        //查询dao数据
        if (hasKey){
            //命中了
            String workerJson = opsForValue.get(workerKey);
            //反序列化
            return JSON.toJavaObject(JSON.parseObject(workerJson),WorkerBO.class);
        }else {
            //没命中
            Worker worker=workerDao.selectByUserId(userId);
            WorkerBO workerBO = workerConverter.po2bo(worker);
            //存储缓存,为后续访问提供缓存数据 序列化
            String workerJson = JSON.toJSONString(workerBO);
            opsForValue.set(workerKey,workerJson);
            //仓储层转化成BO
            return workerBO;
        }*/
    }

    @Override
    public void deleteWorker(Long userId) {
        workerDao.delete(userId);
    }

    @Override
    protected String searialize(WorkerBO workerBO) {
        return JSON.toJSONString(workerBO);
    }
    @Override
    protected WorkerBO desearialize(String json) {
        return JSON.toJavaObject(JSON.parseObject(json),WorkerBO.class);
    }
}
